import React from 'react';
import ReactDOM from 'react-dom';
import CMPLoading from './Loading';

let loading = null;
let div = null;

const show = (props) => {
  if (div) return false;
  div = document.createElement('div');
  document.body.appendChild(div);

  // 关闭方法
  const close = () => {
    if (div) {
      ReactDOM.unmountComponentAtNode(div);
      document.body.removeChild(div);
      div = null;
    }
  }

  loading = ReactDOM.render(<CMPLoading {...props} show={true} close={close}></CMPLoading>, div);
}

const hide = () => {
  loading.fnHide();
  // console.log(loading)
  // if (div) {
  //   ReactDOM.unmountComponentAtNode(div);
  //   document.body.removeChild(div);
  //   div = null;
  // }
}

const Loading = {};
Loading.show = show;
Loading.hide = hide;

export default Loading;